using System;
using System.Collections.Generic;
using FwkLight.Domain.Fetching;
using NHibernate;

namespace FwkLight.Domain.Query
{
  public abstract class MultiCriteriaQuery<T, TOutput> : IDomainQuery<T, TOutput> where T : class
  {
    protected IListMultiCriteriaFetcher<T> _criteria;

    protected IList<T> Load()
    {
      return _criteria.LoadList();
    }

    protected IList<T> Load(ISession session)
    {
      return Load(session, false);
    }

    protected IList<T> Load(ISession session, bool includingInactive)
    {
      Initialise(session, includingInactive);
      return _criteria.LoadList();
    }

    public IDomainQuery<T,TOutput> Initialise(ISession session)
    {
      return Initialise(session, false);
    }

    public IDomainQuery<T, TOutput> Initialise ( ISession session, bool includingInactive )
    {
      _criteria = new ListMultiCriteriaFetcher<T>(session);
      if (FetchingStrategy != null)
        FetchingStrategy.ApplyStrategyOn(_criteria);
      ApplyMainFilters(_criteria);
      if (!includingInactive)
        FilterOnlyActive(_criteria);
      Sort(_criteria);
      return this;
    }

    protected virtual IFetchingStrategy<T> FetchingStrategy
    {
      get
      {
        return null;
      }
    }

    public virtual TOutput GetResult()
    {
      return default(TOutput);
    }

    protected abstract void ApplyMainFilters(IListMultiCriteriaFetcher<T> criteria);
    protected abstract void FilterOnlyActive(IListMultiCriteriaFetcher<T> criteria);
    protected abstract void Sort(IListMultiCriteriaFetcher<T> criteria);
  }

  public abstract class MultiCriteriaQuery<T> : MultiCriteriaQuery<T,IList<T>> where T : class
  {
    
  }
}